<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
                    <el-form-item label="卡片类型" prop="type">
                      <el-radio-group v-model="data.type">
                             <el-radio :label="1">普通卡</el-radio>   
                             <el-radio :label="2">稀有卡</el-radio>
                         </el-radio-group>
                    </el-form-item>
                    <el-form-item label="卡片名称" prop="name">
                        <el-input v-model.trim="data.name" placeholder="请填写卡片名称" />
                    </el-form-item>
                    <el-form-item label="卡片图片" prop="image" v-if="data.type!=4">
                        <el-upload
                    :action="$store.state.settings.file_upload_url"
                    :headers ="headerObj"
                    :limit=1
                    list-type="picture-card"
                    :file-list="data.imagefileList"
                    accept="image/png, image/jpeg, image/gif, image/jpg"
                    :on-success="handleimageSuccess"
                    :before-upload="beforeimageUpload"
                    :on-exceed="handleimageExceed"
                    :on-preview="handleimagePreview"
                    :on-remove="handleimageRemove">
                    <i class="el-icon-plus"></i>
                    </el-upload>
                    </el-form-item>
             
                    <el-form-item label="库存" prop="stock" v-if="data.type == 2">
                        <el-input-number v-model="data.stock" :min="-1"  label="库存"></el-input-number>
                        <div style="color:#999">总库存  -1代表无限制</div>
                    </el-form-item>

                    <el-form-item label="每日发放数" prop="day_stock" v-if="data.type == 2">
                        <el-input-number v-model="data.day_stock" :min="-1"  label="每日发放数"></el-input-number>
                         <div style="color:#999">每日  -1代表无限制</div>
                    </el-form-item>
                    <el-form-item label="中奖次数">
                         <el-radio-group v-model="data.win_times">
                             <el-radio :label="1">只中1次</el-radio>
                             <el-radio :label="-1">不限次数</el-radio>
                         </el-radio-group>
                    </el-form-item>
                    <el-form-item label="中奖概率" prop="ratio">
                        <el-input-number v-model="data.ratio" :min="0"  label="中奖概率"></el-input-number>
                        <div style="color:#999">中奖概率=当前卡片概率/所有卡片中奖概率之和中奖概率=当前卡片概率/所有卡片中奖概率之和</div>
                    </el-form-item>
       
       

    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { create } from '@/api/card'
import { getToken } from '@/utils/auth'


export default {
  name: 'cardCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },

  data() {
    return {
      loading:false,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
            'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    type: [ 
                { required: true, message: "请输入卡片类型", trigger: "blur" }
            ], 
            name: [ 
                { required: true, message: "请输入卡片名称", trigger: "blur" }
            ], 

      },
      data: {
             type:1,
            name: '',
            image:[],
            imagepreviewurl:'',
            imagefileList:[],
            value:'',
            stock:0,
            day_stock:1,
            win_times: 1,
            ratio:'',
            sort:'',
            status:1,

      }
    }
  },
  created() {
  },
  methods: {
    
                handleimageExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handleimagePreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handleimageRemove(){
                 this.data.image = '';
             },
             handleimageSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.image= response.data.src;
            },
             beforeimageUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                this.$message.error("上传图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                this.$message.error("上传图片大小不能超过 2MB!");
                }
                
            },

    
    submit() {
 
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        create({
                       type:this.data.type,
            name:this.data.name,
            image:this.data.image,
            value:this.data.value,
            stock:this.data.stock,
            day_stock:this.data.day_stock,
            win_times:this.data.win_times,
            ratio:this.data.ratio,
            sort:this.data.sort,
            status:this.data.status ?1:0,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
                thiz.data.imagefileList = []
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
